<?php

namespace app\api\controller;
use app\common\controller\Api;
use app\common\model\Area;
use app\common\model\Lights;
use app\manystore\model\ManystoreShop;
use think\Db;

/**
 * 门店
 */
class Shop extends Api
{
    protected $noNeedLogin = ['getShop','shop_list'];
    protected $noNeedRight = '*';
    protected $model = null;
    public function _initialize()
    {
        parent::_initialize();
        $this->model = new ManystoreShop();
    }


    public function shop_list()
    {

//        $latitude = input('latitude');
//        $longitude = input('longitude');

//        $Common = new Common();
//        $response = $Common->getLocation($latitude, $longitude);
//        $responseData = json_decode($response->getContent(), true);
//        if($responseData['code']!=1){
//            $this->error($responseData['msg']);
//        }
//        if(input('type')!=2){
//            //$city_id= Area::where('name','=',$responseData['data']['city'])->value('id');
//
//        }

        $latitude = input('latitude');
        $name = input('name');
        $longitude = input('longitude');
        $query = Db::name('manystore_shop')->alias('ml')
            ->where('status', '=', 1)
//            ->where('city', '=', input('city_id'))
            ->where('longitude', '<>', '')->where('latitude', '<>', '')
            ->field("id,name,image,tel,address,(6378.138 * 2 * asin(sqrt(pow(sin((latitude * pi() / 180 - " . $latitude . " * pi() / 180) / 2),2) + cos(latitude * pi() / 180) * cos(" . $latitude . " * pi() / 180) * pow(sin((longitude * pi() / 180 - " . $longitude . " * pi() / 180) / 2),2))) * 1000) as distance")
            ->order('distance');
        // 如果 $name 有值，则添加模糊查询条件
        if (!empty($name)) {
            $query->where('name', 'like', '%' . $name . '%');
        }

        if (input('city_id')) {
            $query->where('city', '=', input('city_id'));
        }
        $list = $query->paginate(10)->toArray();
        foreach ($list['data'] as $k => $v) {
            $list['data'][$k]['distance'] = round($v['distance'] / 1000, 2) . 'Km';
        }
        $this->success('请求成功', $list);
    }

    public function light_code(){
        $info=Lights::where('id',input('light_id'))->field('code')->find();
        if($info){
            $this->success('请求成功', $info);
        }else{
            $this->error([]);
        }

    }


    public function getShop($id = null, $latitude = null, $longitude = null)
    {
        if ($id) {
            // 根据 ID 查询店铺
            $shop = ManystoreShop::field('id,name,image,address_detail,tel')->where('id', $id)->find();

        } elseif ($latitude && $longitude) {
            // 根据经纬度查询最近的店铺
            $shop = ManystoreShop::
            field( 'id,name,image,address_detail,(6371 * acos(cos(radians(' . $latitude . ')) * cos(radians(latitude)) * cos(radians(longitude) - radians(' . $longitude . ')) + sin(radians(' . $latitude . ')) * sin(radians(latitude)))) AS distance')
                ->order('distance', 'asc')
                ->limit(1)
                ->find();

        } else {
            $this->error('请传参数');
        }
        $this->success('请求成功', $shop);
    }

}
